package cn.stylefeng.guns.modular.yingbao.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.SysSell;
import cn.stylefeng.guns.modular.yingbao.entity.SysUser;
import cn.stylefeng.guns.modular.yingbao.model.params.SysSellParam;
import cn.stylefeng.guns.modular.yingbao.service.SysSellService;
import cn.stylefeng.guns.modular.yingbao.service.SysUserGroupService;
import cn.stylefeng.guns.modular.yingbao.service.SysUserService;
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 挂售控制器
 *
 * @author jj
 * @Date 2019-12-11 09:37:04
 */
@CrossOrigin
@Controller
@RequestMapping("/sysSell")
@Api(tags = "订单，挂售模块")
public class SysSellController extends BaseController {

    private String PREFIX = "/sysSell";

    @Autowired
    private SysSellService sysSellService;
    @Autowired
    private UserService userService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 跳转到主页面
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/sysSell.html";
    }

    /**
     * 新增页面
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping("/add")
    public String add(Model model) {
        Long userId = LoginContextHolder.getContext().getUserId();
        User user = this.userService.getById(userId);
        model.addAttribute("id",userId);
        model.addAttribute("roleName", ConstantFactory.me().getRoleName(user.getRoleId()));
        return PREFIX + "/sysSell_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/sysSell_edit.html";
    }

    /**
     * 跳转页面
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping("/app")
    public String app() {
        return PREFIX + "/sysSellApp.html";
    }

    /**
     * 新增接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/addItem",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增挂售")
    public ResponseData addItem(SysSellParam sysSellParam) {
        return this.sysSellService.add(sysSellParam);
    }

    /**
     * 新增接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/appaddItem",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "app端新增" )
    public ResponseData appAddItem(SysSellParam sysSellParam) {
        return sysSellService.appAddItem(sysSellParam);
    }

    /**
     * 编辑接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(SysSellParam sysSellParam) {
        this.sysSellService.update(sysSellParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping( value = "/editData",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "可批量审批" )
    public ResponseData editData(String ids ,Integer type,String remark) {
        this.sysSellService.editData(ids,type,remark);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(SysSellParam sysSellParam) {
        this.sysSellService.delete(sysSellParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(SysSellParam sysSellParam) {
        SysSell detail = this.sysSellService.getById(sysSellParam.getId());
        String standby1 = detail.getStandby1();
        if ("1".equals(standby1)){
            detail.setStandby1("平台");
        }else {
            Long buyerUserid = detail.getBuyerUserid();
            SysUser sysUser = sysUserService.getById(buyerUserid);
            if (sysUser!=null){
                detail.setStandby1(String.valueOf(sysUser.getReferrerCode()));
            }
        }
        SysUser sysUser = sysUserService.getById(detail.getSellerUserid());
        if (sysUser!=null){
            detail.setSellerUserid(sysUser.getReferrerCode());
        }else {
            detail.setSellerUserid(null);
        }
        return ResponseData.success(detail);
    }

    /**
     * 查看详情接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/appDetail",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("查询合约")
    public ResponseData appDetail(int pageNo,int pageSize,Integer contractType) {
        return this.sysSellService.lookcnt(pageNo,pageSize,contractType);
    }

    /**
     * 购买接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/appBuy",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("购买合约")
    public ResponseData appBuy(Long userid,String ordinal) {
        return this.sysSellService.appBuy(userid,ordinal);
    }


    /**
     * 查看买入合约接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/appContractDetail",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("查询合约详情")
    public ResponseData appContractDetail(Long userid,String ordinal) {
        return this.sysSellService.appContractDetail(userid,ordinal);
    }

    /**
     *
     * 平台拒绝接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/refuse",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("平台拒绝")
    public ResponseData refuse(String ordinal,Long buyerUserid ) {
        return this.sysSellService.refuse(ordinal,buyerUserid);
    }

    /**
     * 付款接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/appPay",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("支付买入合约")
    public ResponseData appPay(SysSell sysSell) {
        return this.sysSellService.appPay(sysSell);
    }

    /**
     * 买家查看收款信息
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/appSellCheckPayment",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("买方根据状态查看合约")
    public ResponseData appSellCheckPayment(Long userid,String stuats) {
        return this.sysSellService.appSellCheckPayment(userid,stuats);
    }

    /**
     * 买家查看收款信息
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/appBuyCheckPayment",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("卖方根据状态查看合约")
    public ResponseData appBuyCheckPayment(Long userid,String stuats) {
        return this.sysSellService.appBuyCheckPayment(userid,stuats);
    }
    /**
     *
     *卖家确认接口
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/appAffirm",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("确认收款合约")
    public ResponseData appAffirm(String ordinal,Long userid) {
        return this.sysSellService.appAffirm(ordinal,userid);
    }


    /**
     *
     * 卖家申诉接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/appComplain",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("卖家申诉")
    public ResponseData appComplain(String ordinal,Long userid ,String phone,String title,String complainDetails) {
        return this.sysSellService.appComplain(ordinal,userid,phone,title,complainDetails);
    }

    /**
     *
     * 卖家申诉接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/platformApproval",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("卖家申诉,平台审批")
    public ResponseData platformApproval(String ordinal,Integer type,String remark) {
        return this.sysSellService.platformApproval(ordinal,type,remark);
    }


    /**
     * 下架接口
     *
     * @author jj
     * @Date 2019-12-11
     */
    @RequestMapping(value = "/unshelve",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("下架接口")
    public ResponseData unshelve(Long id) {
        return this.sysSellService.unshelve(id);
    }

    /**
     * 查询列表
     *
     * @author jj
     * @Date 2019-12-11
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(SysSellParam sysSellParam) {
        //model.addAttribute()
        return this.sysSellService.findPageBySpec(sysSellParam);
    }

    /*  *//**
     * 查看付款详情合约接口
     *
     * @author jj
     * @Date 2019-12-11
     *//*
    @RequestMapping(value = "/appPayDetail",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("查询买入合约")
    public ResponseData appPayDetail(Long userid) {
        return this.sysSellService.appPayDetail(userid);
    }*/

}


